/*
 * SingleMessagePanel.java
 *
 * Created on 24 Sep, 2011, 1:05:45 PM
 */

package net.sf.sbv.ui;

import java.awt.Color;
import net.sf.sbv.modals.Message;
import net.sf.sbv.modals.messageUI.MessageTheme;

/**
 *
 * @author Pratik
 */
public class SingleMessagePanel extends javax.swing.JPanel {
    private Message message;

    /** Creates new form SingleMessagePanel */
    public SingleMessagePanel(Message message) {
        this.message = message;
        initComponents();

        updateMessageData();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbMessage = new javax.swing.JLabel();

        setBackground(new java.awt.Color(204, 255, 204));
        setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                formMouseExited(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                formMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                formMouseReleased(evt);
            }
        });

        lbMessage.setText("jLabel1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbMessage, javax.swing.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lbMessage)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMousePressed
        selectedState();
    }//GEN-LAST:event_formMousePressed

    private void formMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseExited
        normalState();
    }//GEN-LAST:event_formMouseExited

    private void formMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseReleased
        normalState();
    }//GEN-LAST:event_formMouseReleased

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        SingleMessageDialog.showMessageDialog(NewGUI.NEW_GUI, message);
    }//GEN-LAST:event_formMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel lbMessage;
    // End of variables declaration//GEN-END:variables

    private void updateMessageData() {
        normalState();
        this.lbMessage.setText(getHeaderText());
    }

    private String getHeaderText() {
        StringBuilder sb = new StringBuilder("<html><body>");
        sb.append(message.getTimeStamp());
        sb.append("<br/><hr/>");
        sb.append(message.getConvertedBRTagText());
        sb.append("</body></html>");
        return sb.toString();
    }

    private void selectedState() {
        Color backgroundColor = null;
        if(message.getMessageType() == Message.RECEIVED) {
            backgroundColor = MessageTheme.getDefaultTheme().getReceivedPressedColor();
        } else {
            backgroundColor = MessageTheme.getDefaultTheme().getSentPressedColor();
        }

        this.setBackground(backgroundColor);
    }

    private void normalState() {
        Color backgroundColor = null;
        if(message.getMessageType() == Message.RECEIVED) {
            backgroundColor = MessageTheme.getDefaultTheme().getReceivedMessageColor();
        } else {
            backgroundColor = MessageTheme.getDefaultTheme().getSentMessageColor();
        }

        this.setBackground(backgroundColor);
    }
}
